package com.cskaoyan.service;


import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.bo.wx.WxBasicQueryBo;
import com.cskaoyan.bean.bo.wx.WxCouponMylistBo;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.bean.vo.grouponvo.GrouponVo;
import com.cskaoyan.bean.vo.grouponvo.ItemsVo;
import com.cskaoyan.bean.vo.grouponvo.RulesVo;
import com.cskaoyan.bean.vo.grouponvo.SubGrouponsVo;
import com.cskaoyan.bean.vo.wx.WxBaseRespVo;
import com.cskaoyan.bean.vo.wx.WxGrouponMyVo;
import com.cskaoyan.bean.vo.wx.WxMyCouponMylistVo;
import com.cskaoyan.bean.vo.wx.mygroupon.WxGoodsVo;
import com.cskaoyan.bean.vo.wx.mygroupon.WxGrouponVo;
import com.cskaoyan.bean.vo.wx.mygroupon.WxRulesVo;
import com.cskaoyan.dao.*;
import com.cskaoyan.utils.OrderStateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: lsy
 * @class: PromotionServiceImpl
 */
//@CacheConfig(cacheNames = "promotion")
@Transactional(rollbackFor = Exception.class)
@Service
public class PromotionServiceImpl implements PromotionService {

    @Autowired
    AdMapper adMapper;

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    TopicMapper topicMapper;

    @Autowired
    GrouponMapper grouponMapper;

    @Autowired
    GrouponRulesMapper grouponRulesMapper;

    @Autowired
    CouponUserMapper couponUserMapper;

    @Autowired
    GoodsService goodsService;

    @Autowired
    OrderService orderService;

    @Autowired
    UserService userService;

//    @Cacheable(value = "promotion.ad")
    @Override
    public AdListVo queryAds(AdListBo adListBo) {
        AdExample adExample = new AdExample();
        adExample.setOrderByClause(adListBo.getSort() + " " + adListBo.getOrder());

        AdExample.Criteria criteria = adExample.createCriteria();
        criteria.andNameLike("%" + (adListBo.getName() == null ? "" : adListBo.getName()) + "%");
        criteria.andContentLike("%" + (adListBo.getContent() == null ? "" : adListBo.getContent()) + "%");
        criteria.andDeletedEqualTo(false);

        PageHelper.startPage(adListBo.getPage(), adListBo.getLimit());
        List<Ad> ads = adMapper.selectByExample(adExample);
        PageInfo<Ad> adPageInfo = new PageInfo<>(ads);
        int total = (int) adPageInfo.getTotal();

        AdListVo adListVo = new AdListVo();
        adListVo.setItems(ads);
        adListVo.setTotal(total);
        return adListVo;

    }

//    @CacheEvict(value = "promotion.ad", allEntries = true)
    @Override
    public AdUpdateVo changeAd(AdUpdateBo adUpdateBo) {
        AdExample adExample = new AdExample();
        AdExample.Criteria criteria = adExample.createCriteria();
        criteria.andIdEqualTo(adUpdateBo.getId());

        Ad ad = new Ad();
        // 添加时间
        ad.setAddTime(adUpdateBo.getAddTime());
        // 广告内容
        ad.setContent(adUpdateBo.getContent());
        // 逻辑删除
        ad.setDeleted(adUpdateBo.isDeleted());
        // 是否启用
        ad.setEnabled(adUpdateBo.isEnabled());
        // 活动链接
        ad.setLink(adUpdateBo.getLink());
        // 广告标题
        ad.setName(adUpdateBo.getName());
        // position ？
        ad.setPosition(adUpdateBo.getPosition());
        // 更新时间
        Date date = new Date();
        ad.setUpdateTime(date);
        // 图片链接
        ad.setUrl(adUpdateBo.getUrl());

        int affectedCount = adMapper.updateByExampleSelective(ad, adExample);
        if (affectedCount == 1) {
            adUpdateBo.setUpdateTime(date);
            return adUpdateBo;
        }
        return null;
    }

//    @Cacheable("promotion.coupon")
    @Override
    public CouponListVo queryCoupons(CouponListBo couponListBo) {
        CouponExample couponExample = new CouponExample();
        couponExample.setOrderByClause(couponListBo.getSort() + " " + couponListBo.getOrder());

        CouponExample.Criteria criteria = couponExample.createCriteria();
        criteria.andNameLike("%" + (couponListBo.getName() == null ? "" : couponListBo.getName()) + "%");
        if (couponListBo.getType() != 0) {
            criteria.andGoodsTypeEqualTo(couponListBo.getType());
        }
        if (couponListBo.getStatus() != 0) {
            criteria.andStatusEqualTo(couponListBo.getStatus());
        }
        criteria.andDeletedEqualTo(false);

        PageHelper.startPage(couponListBo.getPage(), couponListBo.getLimit());
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);
        int total = (int) couponPageInfo.getTotal();

        CouponListVo couponListVo = new CouponListVo();
        couponListVo.setItems(coupons);
        couponListVo.setTotal(total);

        return couponListVo;
    }

//    @CacheEvict(value = "promotion.coupon", allEntries = true)
    @Override
    public int addCoupon(Coupon coupon) {
        return couponMapper.insert(coupon);
    }

//    @Cacheable("promotion.topic")
    @Override
    public TopicListVo queryTopics(TopicListBo topicListBo) {
        TopicExample topicExample = new TopicExample();
        topicExample.setOrderByClause(topicListBo.getSort() + " " + topicListBo.getOrder());

        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andTitleLike("%" + (topicListBo.getTitle() == null ? "" : topicListBo.getTitle()) + "%");
        criteria.andSubtitleLike("%" + (topicListBo.getSubtitle() == null ? "" : topicListBo.getSubtitle()) + "%");
        criteria.andDeletedEqualTo(false);

        PageHelper.startPage(topicListBo.getPage(), topicListBo.getLimit());
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        PageInfo<Topic> adPageInfo = new PageInfo<>(topics);
        int total = (int) adPageInfo.getTotal();

        TopicListVo topicListVo = new TopicListVo();
        topicListVo.setItems(topics);
        topicListVo.setTotal(total);
        return topicListVo;
    }

//    @CacheEvict(value = "promotion.topic", allEntries = true)
    @Override
    public int changeTopic(Topic topic) {
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andIdEqualTo(topic.getId());

        topic.setUpdateTime(new Date());

        return topicMapper.updateByExampleSelective(topic, topicExample);
    }

//    @CacheEvict(value = "promotion.topic", allEntries = true)
    @Override
    public int addTopic(Topic topic) {
        topic.setAddTime(new Date());
        return topicMapper.insert(topic);
    }

//    @Cacheable("promotion.grouponRule")
    @Override
    public GrouponListVo queryGrouponRules(GrouponListBo grouponListBo) {
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        grouponRulesExample.setOrderByClause(grouponListBo.getSort() + " " + grouponListBo.getOrder());

        GrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();
        if (grouponListBo.getGoodsId() != null) {
            criteria.andGoodsIdEqualTo(grouponListBo.getGoodsId());
        }
        criteria.andDeletedEqualTo(false);

        PageHelper.startPage(grouponListBo.getPage(), grouponListBo.getLimit());
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);
        PageInfo<GrouponRules> adPageInfo = new PageInfo<>(grouponRules);
        int total = (int) adPageInfo.getTotal();

        GrouponListVo grouponListVo = new GrouponListVo();
        grouponListVo.setItems(grouponRules);
        grouponListVo.setTotal(total);

        return grouponListVo;
    }

//    @CacheEvict(value = "promotion.grouponRule", allEntries = true)
    @Override
    public int changeGrouponRules(GrouponRules grouponRules) {
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        GrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();
        criteria.andIdEqualTo(grouponRules.getId());
        grouponRules.setUpdateTime(new Date());

//        grouponRules.setUpdateTime(new Date());
        return grouponRulesMapper.updateByExampleSelective(grouponRules, grouponRulesExample);
    }

//    @Cacheable("promotion.coupon")
    @Override
    public Coupon queryCoupon(int id) {
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andIdEqualTo(id);

        return couponMapper.selectByExample(couponExample).get(0);
    }

//    @CacheEvict(value = "promotion.coupon", allEntries = true)
    @Override
    public int changeCoupon(Coupon coupon) {
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        criteria.andIdEqualTo(coupon.getId());

        coupon.setUpdateTime(new Date());
        return couponMapper.updateByExampleSelective(coupon, couponExample);
    }

//    @CacheEvict(value = "promotion.grouponRule", allEntries = true)
    @Override
    public int addGrouponRules(GrouponRules grouponRules) {

        grouponRules.setAddTime(new Date());
        return grouponRulesMapper.insert(grouponRules);
    }

    @Override
    public GrouponListRecordVo queryGroupon(GrouponListRecordBo grouponListRecordBo, List<Integer> orderIdList) {
        GrouponExample grouponExample = new GrouponExample();
        grouponExample.setOrderByClause(grouponListRecordBo.getSort() + " " + grouponListRecordBo.getOrder());

        GrouponExample.Criteria criteria = grouponExample.createCriteria();
        if (orderIdList.size() != 0) {
            criteria.andOrderIdIn(orderIdList);
        }
        criteria.andDeletedEqualTo(false);

        PageHelper.startPage(grouponListRecordBo.getPage(), grouponListRecordBo.getLimit());
        List<Groupon> groupons = grouponMapper.selectByExample(grouponExample);
        PageInfo<Groupon> grouponPageInfo = new PageInfo<>(groupons);
        int total = (int) grouponPageInfo.getTotal();

        GrouponListRecordVo grouponListRecordVo = new GrouponListRecordVo();
        ArrayList<ItemsVo> items = new ArrayList<>();

        if (orderIdList.size() == 0) {
            grouponListRecordVo.setTotal(0);
            grouponListRecordVo.setItems(items);
            return grouponListRecordVo;
        }

        grouponListRecordVo.setTotal(total);

        for (Groupon groupon : groupons) {
            GrouponVo grouponVo = new GrouponVo();
            grouponVo.setOrderId(groupon.getOrderId());
            grouponVo.setUserId(groupon.getUserId());
            grouponVo.setShareUrl(groupon.getShareUrl());

            RulesVo rulesVo = new RulesVo();
            rulesVo.setAddTime(groupon.getAddTime());

            GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
            if (groupon.getRulesId() != 0) {
                grouponRulesExample.createCriteria().andIdEqualTo(groupon.getRulesId()).andDeletedEqualTo(false);
            }
            GrouponRules grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample).get(0);
            rulesVo.setDiscount(grouponRules.getDiscount());
            rulesVo.setDiscountMember(grouponRules.getDiscountMember());
            rulesVo.setExpireTime(grouponRules.getExpireTime());

            SubGrouponsVo subGrouponsVo = new SubGrouponsVo();

            // 获取同一个团的 userId orderId 的list 特征，相同的grouponId
            ArrayList<SubGrouponsVo> subGrouponsVos = new ArrayList<>();
            GrouponExample grouponExample1 = new GrouponExample();
            grouponExample1.createCriteria().andGrouponIdEqualTo(groupon.getGrouponId()).andDeletedEqualTo(false);
            List<Groupon> grouponsList = grouponMapper.selectByExample(grouponExample1);
            for (Groupon groupon1 : grouponsList) {
                SubGrouponsVo subGrouponsVo1 = new SubGrouponsVo();
                subGrouponsVo1.setOrderId(groupon1.getOrderId());
                subGrouponsVo1.setUserId(groupon1.getUserId());
                subGrouponsVos.add(subGrouponsVo1);
            }

            ItemsVo itemsVo = new ItemsVo();
            itemsVo.setGroupon(grouponVo);
            itemsVo.setSubGroupons(subGrouponsVos);
            itemsVo.setRules(rulesVo);
            items.add(itemsVo);
        }
        grouponListRecordVo.setItems(items);
        return grouponListRecordVo;
    }

//    @CacheEvict(value = "promotion.ad", allEntries = true)
    @Override
    public int addAd(Ad ad) {
        return adMapper.insertSelective(ad);
    }

//    @Cacheable("promotion.couponUser")
    @Override
    public CouponListuserVo queryUsersByCouponIdAndUserIdAndStatus(CouponListuserBo couponListuserBo) {
        CouponUserExample couponUserExample = new CouponUserExample();
        couponUserExample.setOrderByClause(couponListuserBo.getSort() + " " + couponListuserBo.getOrder());

        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        criteria.andCouponIdEqualTo(couponListuserBo.getCouponId());
        if (couponListuserBo.getUserId() != 0) {
            criteria.andUserIdEqualTo(couponListuserBo.getUserId());
        }
        if (couponListuserBo.getStatus() != 0) {
            criteria.andStatusEqualTo(couponListuserBo.getStatus());
        }
        criteria.andDeletedEqualTo(false);

        PageHelper.startPage(couponListuserBo.getPage(), couponListuserBo.getLimit());
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);
        PageInfo<CouponUser> couponUserPageInfo = new PageInfo<>(couponUsers);
        int total = (int) couponUserPageInfo.getTotal();

        ArrayList<CouponUser> items = new ArrayList<>();
        items.addAll(couponUsers);
        CouponListuserVo couponListuserVo = new CouponListuserVo();
        couponListuserVo.setTotal(total);
        couponListuserVo.setItems(items);
        return couponListuserVo;


    }

//    @Cacheable("promotion.ad")
    @Override
    public List<Ad> queryAllAds() {
        AdExample adExample = new AdExample();
        AdExample.Criteria criteria = adExample.createCriteria();
        // 没有删除的
        criteria.andDeletedEqualTo(false);
        // 启用的
        criteria.andEnabledEqualTo(true);
        return adMapper.selectByExample(adExample);
    }

//    @Cacheable("promotion.coupon")
    @Override
    public List<Coupon> queryAllCoupons() {
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        return couponMapper.selectByExample(couponExample);
    }

    @Override
    public List<com.cskaoyan.bean.vo.wx.GrouponVo> queryAllGroupons() {
        GrouponExample grouponExample = new GrouponExample();
        GrouponExample.Criteria criteria = grouponExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Groupon> groupons = grouponMapper.selectByExample(grouponExample);
        List<com.cskaoyan.bean.vo.wx.GrouponVo> grouponList = new ArrayList<>();
        for (Groupon groupon : groupons) {
            int orderId = groupon.getOrderId();
            List<OrderGoods> orderGoodsList = goodsService.queryOrderGoodsByOrderId(orderId);

            for (OrderGoods orderGoods : orderGoodsList) {
                Goods goods = goodsService.queryGoodsDetail(orderGoods.getGoodsId()).getGoods();
                Order order = goodsService.queryOrderByOrderId(orderId);

                GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
                GrouponRulesExample.Criteria criteria1 = grouponRulesExample.createCriteria();
                criteria1.andDeletedEqualTo(false);
                if (groupon.getRulesId() != 0) {
                    criteria1.andIdEqualTo(groupon.getRulesId());
                }
                GrouponRules grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample).get(0);

                com.cskaoyan.bean.vo.wx.GrouponVo grouponVo = new com.cskaoyan.bean.vo.wx.GrouponVo();
                grouponVo.setGroupon_member(grouponRules.getDiscountMember());
                grouponVo.setGoods(goods);
                try {
                    grouponVo.setGroupon_price(order.getGrouponPrice());
                } catch (Exception ignore) {

                }
                grouponList.add(grouponVo);
            }


        }

        return grouponList;
    }

//    @Cacheable("promotion.topic")
    @Override
    public List<Topic> queryAllTopics() {
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        return topicMapper.selectByExample(topicExample);
    }

    @Override
    public WxMyCouponMylistVo queryWxCouponsByUserId(WxCouponMylistBo wxCouponMylistBo, int userId) {
        CouponUserExample couponUserExample = new CouponUserExample();
        CouponUserExample.Criteria criteria1 = couponUserExample.createCriteria();
        if (userId != 0) {
            criteria1.andUserIdEqualTo(userId);
        }
        criteria1.andStatusEqualTo(wxCouponMylistBo.getStatus());
        List<CouponUser> couponUserList = couponUserMapper.selectByExample(couponUserExample);
        ArrayList<Integer> couponIdList = new ArrayList<>();
        for (CouponUser couponUser : couponUserList) {
            couponIdList.add(couponUser.getCouponId());
        }

        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
//        criteria.andStatusEqualTo(wxCouponMylistBo.getStatus());
        criteria.andIdIn(couponIdList);
        criteria.andDeletedEqualTo(false);

        PageHelper.startPage(wxCouponMylistBo.getPage(), wxCouponMylistBo.getSize());
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);

        for (int i = 0; i < coupons.size(); i++) {
            Coupon coupon = coupons.get(i);
            coupon.setStartTime(couponUserList.get(i).getStartTime());
            coupon.setEndTime(couponUserList.get(i).getEndTime());
        }

        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);
        int total = (int) couponPageInfo.getTotal();

        WxMyCouponMylistVo wxMyCouponMylistVo = new WxMyCouponMylistVo();
        wxMyCouponMylistVo.setCount(total);
        wxMyCouponMylistVo.setData(coupons);
        return wxMyCouponMylistVo;
    }

//    @CacheEvict(value = "promotion.couponUser", allEntries = true)
    @Override
    public int addWxCouponByCode(String code, int userId) {
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        criteria.andCodeEqualTo(code);
        criteria.andDeletedEqualTo(false);

        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        if (coupons.size() == 0) {
            return 0;// 兑换码错误
        }
        Coupon coupon = coupons.get(0);

        CouponUserExample couponUserExample = new CouponUserExample();
        CouponUserExample.Criteria criteria1 = couponUserExample.createCriteria();
        criteria1.andCouponIdEqualTo(coupon.getId());
        criteria1.andDeletedEqualTo(false);
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);
        if (couponUsers.size() != 0) {
            return 2;// 已兑换
        }

        Date date = new Date();
        CouponUser couponUser = new CouponUser();
        couponUser.setUserId(userId);
        couponUser.setAddTime(date);
        couponUser.setCouponId(coupon.getId());
        couponUser.setDeleted(false);
        couponUser.setOrderId(null);

        // 根据coupon.timeType分类
        if (coupon.getTimeType() == 0) {
            // 没有startTime和endTime
            couponUser.setStartTime(date);
            couponUser.setEndTime(new Date(date.getTime() + coupon.getDays() * 24 * 60 * 60 *1000));
        } else {
            couponUser.setStartTime(coupon.getStartTime());
            couponUser.setEndTime(coupon.getEndTime());
        }

        // 未使用
        couponUser.setStatus((short)0);
        couponUser.setUpdateTime(date);
        couponUser.setUsedTime(null);

        return couponUserMapper.insert(couponUser);
    }

//    @Cacheable("promotion.couponUser")
    @Override
    public CouponUser queryCouponUserByCouponIdAndUserId(int couponId, int userId) {
        CouponUserExample couponUserExample = new CouponUserExample();
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        criteria.andCouponIdEqualTo(couponId);
        criteria.andUserIdEqualTo(userId);

        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);
        if (couponUsers.size() == 0) {
            return null;
        }
        return couponUsers.get(0);

    }

//    @CacheEvict(value = "promotion.couponUser", allEntries = true)
    @Override
    public int addCouponUserByUserIdAndCoupon(Coupon coupon, Integer userId) {
        CouponUser couponUser = new CouponUser();
        couponUser.setUserId(userId);
        couponUser.setCouponId(coupon.getId());
        couponUser.setStatus((short)0);
        couponUser.setUsedTime(null);
        Date date = new Date();

        // 根据coupon.timeType分类
        if (coupon.getTimeType() == 0) {
            // 没有startTime和endTime
            couponUser.setStartTime(date);
            couponUser.setEndTime(new Date(date.getTime() + coupon.getDays() * 24 * 60 * 60 *1000));
        } else {
            couponUser.setStartTime(coupon.getStartTime());
            couponUser.setEndTime(coupon.getEndTime());
        }

        couponUser.setOrderId(null);

        couponUser.setAddTime(date);
        couponUser.setUpdateTime(date);
        couponUser.setDeleted(false);

        return couponUserMapper.insert(couponUser);
    }

//    @Cacheable("promotion.coupon")
    @Override
    public WxBaseRespVo<Coupon> queryCouponsByPage(WxBasicQueryBo wxBasicQueryBo) {
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        PageHelper.startPage(wxBasicQueryBo.getPage(), wxBasicQueryBo.getSize());
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);
        int total = (int) couponPageInfo.getTotal();

        WxBaseRespVo<Coupon> wxBaseRespVo = new WxBaseRespVo<>();
        wxBaseRespVo.setData(coupons);
        wxBaseRespVo.setCount(total);
        return wxBaseRespVo;
    }

    // 复杂不用缓存
    @Override
    public WxBaseRespVo<WxGrouponMyVo> queryGrouponByUserIdAndShowType(int showType, int userId) {
        GrouponExample grouponExample = new GrouponExample();
        GrouponExample.Criteria criteria = grouponExample.createCriteria();
        if (showType == 0) {
            criteria.andCreatorUserIdEqualTo(userId);
        } else {
            criteria.andUserIdEqualTo(userId);
        }
        criteria.andDeletedEqualTo(false);

        List<Groupon> groupons = grouponMapper.selectByExample(grouponExample);

        WxBaseRespVo<WxGrouponMyVo> wxBaseRespVo = new WxBaseRespVo<>();
        wxBaseRespVo.setCount(groupons.size());
        ArrayList<WxGrouponMyVo> data = new ArrayList<>();

        for (Groupon groupon : groupons) {
            Integer orderId = groupon.getOrderId();
            Order order = goodsService.queryOrderByOrderId(orderId);


            WxGrouponMyVo wxgrouponmyVo = new WxGrouponMyVo();
            try {
                wxgrouponmyVo.setActualPrice(order.getActualPrice());
            } catch (Exception ignore) {

            }

            User user = userService.queryUserById(groupon.getCreatorUserId());
            // 设置昵称为创建者
            try {
                wxgrouponmyVo.setCreator(user.getNickname());
            } catch (Exception ignore) {
            }

            List<OrderGoods> orderGoodsList = goodsService.queryOrderGoodsByOrderId(orderId);
            ArrayList<WxGoodsVo> wxGoodsVos = new ArrayList<>();
            for (OrderGoods orderGoods : orderGoodsList) {
                WxGoodsVo wxGoodsVo = new WxGoodsVo();
                wxGoodsVo.setPicUrl(orderGoods.getPicUrl());
                wxGoodsVo.setGoodsName(orderGoods.getGoodsName());
                wxGoodsVo.setNumber(orderGoods.getNumber());
                wxGoodsVos.add(wxGoodsVo);
            }
            wxgrouponmyVo.setGoodsList(wxGoodsVos);

            WxGrouponVo wxGrouponVo = new WxGrouponVo();
            wxGrouponVo.setAddTime(groupon.getAddTime());
            wxgrouponmyVo.setGroupon(wxGrouponVo);

            wxgrouponmyVo.setId(groupon.getId());

            wxgrouponmyVo.setIsCreator(showType == 0);

            // 根据团购id确定有多少人参团
            int joinerCount = grouponMapper.selectJoinerCountByGrouponId(groupon.getGrouponId());
            wxgrouponmyVo.setJoinerCount(joinerCount);

            try {
                wxgrouponmyVo.setOrderSn(order.getOrderSn());
                String statusText = OrderStateUtil.getStatusText(order.getOrderStatus());
                wxgrouponmyVo.setOrderStatusText(statusText);
            } catch (Exception ignore) {
            }


            GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
            GrouponRulesExample.Criteria criteria1 = grouponRulesExample.createCriteria();
            if (groupon.getRulesId() != 0) {
                criteria1.andIdEqualTo(groupon.getRulesId());
            }
            criteria1.andDeletedEqualTo(false);
            List<GrouponRules> grouponRulesList = grouponRulesMapper.selectByExample(grouponRulesExample);
            GrouponRules grouponRules = grouponRulesList.get(0);
            WxRulesVo wxRulesVo = new WxRulesVo();
            wxRulesVo.setDiscount(grouponRules.getDiscount());
            wxRulesVo.setDiscountMember(grouponRules.getDiscountMember());
            wxgrouponmyVo.setRules(wxRulesVo);
            data.add(wxgrouponmyVo);
        }
        wxBaseRespVo.setData(data);

        return wxBaseRespVo;
    }

//    @Cacheable("promotion.coupon")
    @Override
    public Groupon queryGrouponByUserIdAndGrouponId(int grouponId, int userId) {
        GrouponExample grouponExample = new GrouponExample();
        GrouponExample.Criteria criteria = grouponExample.createCriteria();
        criteria.andGrouponIdEqualTo(grouponId);
        criteria.andUserIdEqualTo(userId);
        criteria.andDeletedEqualTo(false);

        List<Groupon> groupons = grouponMapper.selectByExample(grouponExample);
        if (groupons.size() == 0) {
            return null;
        }
        return groupons.get(0);
    }

//    @Cacheable("promotion.groupon")
    @Override
    public List<Groupon> queryGrouponByOrderId(int orderId) {
        GrouponExample grouponExample = new GrouponExample();
        GrouponExample.Criteria criteria = grouponExample.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        criteria.andDeletedEqualTo(false);
        return grouponMapper.selectByExample(grouponExample);
    }

//    @Cacheable("promotion.groupon")
    @Override
    public List<Groupon> queryGrouponByGrouponId(int grouponId) {
        GrouponExample grouponExample = new GrouponExample();
        GrouponExample.Criteria criteria = grouponExample.createCriteria();
        criteria.andGrouponIdEqualTo(grouponId);
        criteria.andDeletedEqualTo(false);

        return grouponMapper.selectByExample(grouponExample);
    }

//    @Cacheable("promotion.groupon")
    @Override
    public Groupon queryGrouponById(int id) {
        GrouponExample grouponExample = new GrouponExample();
        GrouponExample.Criteria criteria = grouponExample.createCriteria();
        criteria.andIdEqualTo(id);
        criteria.andDeletedEqualTo(false);

        List<Groupon> groupons = grouponMapper.selectByExample(grouponExample);
        if (groupons.size() != 0) {
            return groupons.get(0);
        }
        return null;
    }

    @Override
    public GrouponRules querygrouponRulesById(int id) {
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        GrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();
        criteria.andIdEqualTo(id);
        criteria.andDeletedEqualTo(false);

        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);
        if (grouponRules.size() == 0) {
            return null;
        }
        return grouponRules.get(0);
    }

    @Override
    public List<CouponUser> queryCouponUserByUserId(int userId) {
        CouponUserExample couponUserExample = new CouponUserExample();
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andDeletedEqualTo(false);

        return couponUserMapper.selectByExample(couponUserExample);
    }

    @Override
    public Coupon queryCouponById(int id) {
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        if (id == 0) {
            // 不使用优惠券
            return null;
        }
        criteria.andIdEqualTo(id);
        criteria.andDeletedEqualTo(false);

        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        if (coupons.size() == 0) {
            return null;
        }
        return coupons.get(0);
    }

    @Override
    public List<Groupon> queryGroupons() {
        GrouponExample grouponExample = new GrouponExample();
        grouponExample.createCriteria().andDeletedEqualTo(false);
        return grouponMapper.selectByExample(grouponExample);
    }

    @Override
    public int addGroupon(Groupon groupon) {
        return grouponMapper.insertSelective(groupon);
    }

    @Override
    public int changeCouponUserByCouponIdAndUserId(CouponUser couponUser, int couponId, int userId) {
        CouponUserExample couponUserExample = new CouponUserExample();
        couponUserExample.createCriteria().andCouponIdEqualTo(couponId).andUserIdEqualTo(userId);

        return couponUserMapper.updateByExampleSelective(couponUser, couponUserExample);
    }


}
